#encoding:utf-8
import requests
from lxml import etree
BASE_DOMAIN = 'https://www.dytt8.net'
headers = {
    "User-Agent": "User-Agent, Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
    "Referer":"https://www.dytt8.net/html/gndy/dyzz/list_23_1.html"
}

def get_detail_urls(url):
    response = requests.get(url,headers=headers)
    # text = response.content.decode('gbk')
    text = response.text
    html = etree.HTML(text)
    detail_urls = html.xpath("//table[@class='tbspan']//a/@href")
    # def a(url):
    #     return BASE_DOMAIN+url
    # index=0
    # for detail_url in detail_urls:
    #     detail_url = a(detail_url)
    #     detail_urls[index] = detail_url
    #     index+=1
    # print(detail_urls)

    detail_urls = map(lambda url:BASE_DOMAIN+url,detail_urls)
    return detail_urls

def parse_detail_page(url):
    movie = {} #创建一个新的字典用来 存放各个字段的值
    response = requests.get(url,headers=headers)
    text = response.content.decode('gbk')
    html = etree.HTML(text)
    title = html.xpath("//div[@class='title_all']//font[@color='#07519a']/text()")[0]
    movie['title'] = title
    Zooms = html.xpath("//div[@id='Zoom']")[0]
    imgs = Zooms.xpath(".//img/@src")
    cover = imgs[0]
    screenshot = imgs[1]
    movie['cover'] = cover
    movie['screenshot'] = screenshot

    infos = Zooms.xpath(".//text()")
    def parse_info(info,rule):
        return info.replace(rule,"").strip()
    for index,info in enumerate(infos):
        if info.startswith("◎年　　代"):
            info = parse_info(info,"◎年　　代")
            movie['info']=info
        elif info.startswith("◎产　　地"):
            info = parse_info(info, "◎产　　地")
            movie['country'] = info
        elif info.startswith("◎类　　别"):
            info = parse_info(info, "◎类　　别")
            movie['category'] = info
        elif info.startswith("◎豆瓣评分"):
            info = parse_info(info, "◎豆瓣评分")
            movie['douban_rating'] = info
        elif info.startswith("◎片　　长"):
            info = parse_info(info, "◎片　　长")
            movie['duration'] = info
        elif info.startswith("◎导　　演"):
            info = parse_info(info, "◎导　　演")
            movie['director'] = info
        elif info.startswith("◎主　　演"):
            info = parse_info(info, "◎主　　演")
            actors = [info] #先将 ◎主　　演　马东锡 Tong-Seok Ma  放进去
            for x in range(index+1,len(infos)):
                actor = infos[x].strip()
                if actor.startswith("◎"):
                    break
                actors.append(actor)
                # print(actors)
            movie['actors'] = actors
        elif info.startswith("◎简　　介 "):
            info = parse_info(info, "◎简　　介 ")
            for x in range(index+1,len(infos)):
                profile = infos[x].strip()
                movie['profile'] = profile
    download_url = html.xpath("//td[@bgcolor='#fdfddf']/a/text()")[0]
    movie['download_url'] = download_url
    return movie
def spider():
    base_url = "https://www.dytt8.net/html/gndy/dyzz/list_23_{}.html"
    movies = []
    for x in range(1,3):
        url = base_url.format(x)
        detail_urls = get_detail_urls(url)
        for detail_url in detail_urls:
            movie= parse_detail_page(detail_url)
            movies.append(movie)
    print(movies)

if __name__ == "__main__":
    spider()